import os,fnmatch,sys

def all_files(root,patterns='*',single_level=False,yield_folders=False,search_text="sys",replace_text="log"):
    patterns = patterns.split(';');
    newPath = "";
    createdObject = False;
    
    for path,subdirs,files in os.walk(root):
        if yield_folders:
            files.extend(subdirs);

        files.sort();
        for name in files:
            newPath = "D"+(path[1:len(path)]);
            
            if(not os.path.exists(newPath)):
                os.makedirs(newPath);
            
            for pattern in patterns:
                if fnmatch.fnmatch(name,pattern):
                    createObject = False;
                    print name;
                    input_file=open(os.path.join(path,name),'r');                    
                    output_file=open(os.path.join(newPath,name),'w'); 
                    #output_file.write(input_file.readline());
                    #output_file.write("import org.apache.commons.logging.Log;");
                    #output_file.write(os.linesep);                                        
                    #output_file.write("import org.apache.commons.logging.LogFactory;");
                    #output_file.write(os.linesep);                    
                    
                    for s in input_file:
                        output_file.write(s.replace(search_text, replace_text));
                        
                    input_file.close();
                    output_file.close();
                    break;

        if single_level:
            break;
    
            
if __name__=='__main__':
    #all_files('E:\workshop\company\unsins\WebApp\WEB-INF\page','*.jsp',search_text='<%=path%>/sitechannel/service.do?method=toService',replace_text='<%=path%>/statichtml/service/toService.html');
    all_files('E:\workshop\company\unsins\WebApp\pages','*.jsp',search_text='<bean:message key="label.window.title"/>',replace_text='<fmt:message key="label.unsins.title"/>');
##    for path in thefiles:
##        print path;
##    

